let deviceType = "";
// #ifdef H5
deviceType = "h5";
// #endif
// #ifdef MP-WEIXIN
deviceType = process.env.VUE_APP_DEVICE_TYPE;
// #endif
import { subjectInfo } from "@/settings";
import { switchSiteReqOld } from "../services/auth";
import { BASE_URL } from "@/settings";

export default function request(options, config) {
  options = Object.assign(options, {
    header: {
      deviceType: deviceType,
      token: uni.getStorageSync("token")
    }
  });
  const showToast = !(config && config.showToast === false);
  return new Promise((resolve, reject) => {
    uni.request({
      ...options,
      success: res => {
        if (res.data && res.data.code === 200) {
          resolve(res.data.data);
        } else if (res.data.code === 101) {
          showToast &&
            uni.showModal({
              title: "提示",
              showCancel: false,
              content: res.data.msg,
              confirmColor: subjectInfo.theme,
              success: () => {
                switchSiteReqOld(res.data.data)
                  .then(res => {
                    uni.navigateTo({
                      url: `/authorize/index`
                    });
                  })
                  .catch(res => { });
              }
            });
        } else if (res.data.code === 401) {
          // 401 暂时不提示
          uni.removeStorageSync("token");
          // showToast &&
          //   uni.showToast({
          //     title: "token失效",
          //     icon: "none",
          //     duration: 1500,
          //     complete: () => {
          //       uni.removeStorageSync("token");
          //       uni.navigateTo({
          //         url: `/authorize/index`
          //       });
          //     }
          //   });
        } else if (res.data.code === 403) {
          if (res.data.data === 'check') {
            uni.removeStorageSync("token");
          }
          reject(new Error(res.data.msg));
        } else if (res.data.code === 400) {
          showToast &&
            uni.showModal({
              title: "提示",
              showCancel: false,
              content: res.data.msg,
              confirmColor: subjectInfo.theme,
              success: () => {
                if (
                  res.data.msg === "当前登录租户已到期，请联系管理员续期后重试"
                ) {
                  uni.removeStorageSync("token");
                  uni.navigateTo({
                    url: `/authorize/index`
                  });
                }
              }
            });
          reject(new Error(res.data.msg));
        } else {
          showToast &&
            uni.showToast({
              title: res.data.msg,
              icon: "none",
              duration: 2000
            });
          reject(new Error(res.data.msg));
        }
      },
      fail: (err) => {
        let errorInfo = null
        try {
          errorInfo = JSON.stringify({
            options,
            error: err
          })
        } catch (err2) {}

        uni.request({
          header: {
            deviceType: deviceType,
            token: uni.getStorageSync("token")
          },
          url: `${BASE_URL}/hr/api/v1/errorInfos`,
          method: 'post',
          data: {
            deviecType: deviceType,
            errorInfo: errorInfo
          }
        }).catch(() => {})

        showToast &&
          uni.showToast({
            title: "请求失败",
            icon: "none",
            duration: 2000
          });
        reject(new Error("请求失败"));
      },
      complete: (st) => {
        // let errorInfo = null
        // try {
        //   errorInfo = JSON.stringify({
        //     options,
        //     error: st
        //   })
        // } catch (err2) {}

        // uni.request({
        //   header: {
        //     deviceType: deviceType,
        //     token: uni.getStorageSync("token")
        //   },
        //   url: `${BASE_URL}/hr/api/v1/errorInfos`,
        //   method: 'post',
        //   data: {
        //     deviecType: deviceType,
        //     errorInfo: errorInfo
        //   }
        // }).catch(() => {})
      }
    });
  });
}

export function uploadFileReq(options, config) {
  options = Object.assign(options, {
    header: {
      deviceType: deviceType,
      token: uni.getStorageSync("token")
    }
  });
  const showToast = !(config && config.showToast === false);
  return new Promise((resolve, reject) => {
    uni.uploadFile({
      ...options,
      success: res => {
        const resData = JSON.parse(res.data);
        if (resData && resData.code === 200) {
          resolve(resData.data);
        } else {
          showToast &&
            uni.showToast({
              title: resData.msg,
              icon: "none",
              duration: 2000
            });
          reject(new Error(resData.msg));
        }
      },
      fail: err => {
        showToast &&
          uni.showToast({
            title: "请求失败",
            icon: "none",
            duration: 2000
          });
        reject(new Error("请求失败"));
      }
    });
  });
}

export function downloadFileReq(options, config) {
  options = Object.assign(options, {
    header: {
      deviceType: deviceType,
      token: uni.getStorageSync("token")
    }
  });
  const showToast = !(config && config.showToast === false);
  return new Promise((resolve, reject) => {
    uni.downloadFile({
      ...options,
      success: res => {
        resolve(res);
      },
      fail: e => {
        console.log("Error", e);
        showToast &&
          uni.showToast({
            title: "请求失败",
            icon: "none",
            duration: 2000
          });
        reject(new Error("请求失败"));
      }
    });
  });
}
